package com.zhou.hot.agent;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by zhouyongjun on 2020/4/17
 */
@Slf4j
public class HotLoadConfig {
    public static final String FILE = "hot/hotload.properties";
    public   String classesDir = "E:\\gitee\\public\\hot.update\\hot_update\\classes";
    public   String classesConfig = "E:\\gitee\\public\\hot.update\\hot_update\\conf\\hot_update.properties";
    public   String classesHistory = "E:\\gitee\\public\\hot.update\\hot_update\\history";
    
    public boolean load() throws Exception {
        String path = this.getClass().getResource("/").getPath() +FILE;
        log.info("hot load agent 启动.... load hot config ="+path);
        File file = new File(path);
        Properties properties = new Properties();
        FileInputStream in = null;
        try {
            if (!file.exists())
            {
                path = FILE;
                file = new File(FILE);
                log.info("hot load agent 启动.... load hot config2 ="+path);
            }
            in = new FileInputStream(file);
            properties.load(in);
            String value = properties.getProperty("hot.classes.dir");
            if (value != null && !value.isEmpty())
            {
                classesDir = value;
            }
            value = properties.getProperty("hot.classes.config");
            if (value != null && !value.isEmpty())
            {
                classesConfig = value;
            }
            value = properties.getProperty("hot.classes.history");
            if (value != null && !value.isEmpty())
            {
                classesHistory = value;
            }
            return true;
        } catch (Exception e1) {
            log.error(path +"配置文件加载出错",e1);
            throw  e1;
        }
        finally {
            if (in != null) {
                try { in.close(); } catch (IOException e) { } }
        }
    }


    public String getClassesDir() {
        return classesDir;
    }

    public void setClassesDir(String classesDir) {
        this.classesDir = classesDir;
    }

    public String getClassesConfig() {
        return classesConfig;
    }

    public void setClassesConfig(String classesConfig) {
        this.classesConfig = classesConfig;
    }

    public String getClassesHistory() {
        return classesHistory;
    }

    public void setClassesHistory(String classesHistory) {
        this.classesHistory = classesHistory;
    }

    public Properties loadClassesConfigProperties() {
        if (classesConfig == null || classesConfig.isEmpty()) return null;
        File config = new File(classesConfig);
        if (!config.exists()) return null;
        FileInputStream in = null;
        Properties properties = null;
        try {
            properties = new Properties();
            in = new FileInputStream(config);
            properties.load(in);
            return properties;
        } catch (Exception e1) {
            log.error("hot load agent " + classesConfig +"配置文件加载出错",e1);
            return null;
        }
        finally {
            if (in != null) {
                try { in.close(); } catch (IOException e) { } }
        }
    }
}
